﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Net;

namespace EightBeats
{
    [Activity(Label = "Welcome to 8Beats", Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        public override bool OnPrepareOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu, menu);
            return base.OnPrepareOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.menuAuthor:
                    StartActivity(typeof(AuthorActivity));
                    return true;
            }
            return base.OnOptionsItemSelected(item);
        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.btnSelectAudio);
            button.Click += delegate {
                if (Util.IsConnected(this))
                {
                    var audioIntent = new Intent();
                    audioIntent.SetAction(Intent.ActionGetContent);
                    audioIntent.SetType("audio/*");
                    audioIntent.AddCategory(Intent.CategoryOpenable);

                    try
                    {
                        StartActivityForResult(Intent.CreateChooser(audioIntent, "Select Audio (mp3) file"), 0);

                    }
                    catch(Android.Content.ActivityNotFoundException){
                        // Potentially direct the user to the Market with a Dialog
                        Toast.MakeText(this, "Please install a File Manager.", ToastLength.Short).Show();
                    }
                }
                else
                {
                   Toast.MakeText(this, "This action requires your phone to be connected to the internet.", ToastLength.Short).Show();
                }

            };
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);

            if (resultCode == Result.Ok)
            {
                Intent intent = new Intent(this, typeof(ConvertActivity));
                intent.PutExtra("FileToConvert", data.DataString);
                StartActivity(intent);
            }
        }
    }
}

